package com.aurora.oa.work.view;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.aurora.oa.R;
import com.aurora.oa.activity.BaseActivity;
import com.aurora.oa.listener.OnRequestCarListListener;
import com.aurora.oa.listener.OnRequestNoticeListListener;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.utils.ToastUtil;
import com.aurora.oa.work.adapter.ApplyCarListAdapter;
import com.aurora.oa.work.adapter.NoticeAdapter;
import com.aurora.oa.work.bean.NoticeBean;
import com.aurora.oa.work.model.ApplyCarBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/28 0028.
 */
public class ApplyCarListActivity extends BaseActivity{
    RecyclerView recyclerView;
    SwipeRefreshLayout swipeRefreshLayout;
    LinearLayoutManager layoutManager;
    boolean isLoading;
    private int whitchPage =1;
    private List<ApplyCarBean> data = new ArrayList<>();
    private ApplyCarListAdapter adapter = new ApplyCarListAdapter(this,data, true);
    private Handler handler = new Handler();
    RequestHttpModel requestHttpModel = new RequestHttpModelImpl();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitlebar(true,"用车",true);
        setMyContentView(R.layout.activity_apply_car_list);
        initView();
        getData();
    }


    //请求数据
    private void getData(){
        requestHttpModel.getCarListData(whitchPage, new OnRequestCarListListener() {
            @Override
            public void onRequestCarListSuccess(List<ApplyCarBean> carBeanList) {
                if(whitchPage==1){
                    doPullDownRefresh(carBeanList);
                }else{
                    doPullUpLoad(carBeanList);
                }
            }

            @Override
            public void onRequestCarListFail(String message) {
                ToastUtil.show(getApplicationContext(),message);
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }
    private void initView() {

        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.SwipeRefreshLayout);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        swipeRefreshLayout.setColorSchemeResources(R.color.blue,R.color.white);
        swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true);
            }
        });

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        whitchPage = 1;
                        data.clear();
                        getData();
                    }
                }, 0);
            }
        });
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        adapter.setOnApplyCarClickListener(new ApplyCarListAdapter.OnApplyCarClickListener() {
            @Override
            public void onApplyCarClick(View view, int position) {
                Intent intent = new Intent(ApplyCarListActivity.this,ApplyCarDetailActivity.class);
                intent.putExtra("id",data.get(position).getId());
                startActivity(intent);
            }

            @Override
            public void onApplyCarLongClick(View view, int position) {

            }
        });
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                Log.d("test", "StateChanged = " + newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                Log.d("test", "onScrolled");
                loadMoreData();
            }
        });

    }

    //根据位置自动判断是否加载更多数据
    private void loadMoreData(){
        int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
        if (lastVisibleItemPosition + 1 == adapter.getItemCount()) {
            Log.d("test", "loading executed");
            boolean isRefreshing = swipeRefreshLayout.isRefreshing();
            if (isRefreshing) {
                adapter.notifyItemRemoved(adapter.getItemCount());
                return;
            }
            if (!isLoading) {
                isLoading = true;
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        whitchPage +=1;
                        getData();
                        Log.d("test", "load more completed");
                        //     isLoading = false;
                    }
                }, 0);
            }
        }
    }

    public void doPullDownRefresh(List<ApplyCarBean> carList) {
        data.addAll(carList);
        adapter.notifyDataSetChanged();
        adapter.setHasMoreData(true);
        swipeRefreshLayout.setRefreshing(false);
        adapter.notifyItemRemoved(adapter.getItemCount());
        //防止页数太少不加载了，滑动的时候会自动判断，判断静止的时候就自己去加载了，0表示不滑动了
        if(recyclerView.getScrollState()==0){
            loadMoreData();
        }
    }


    public void doPullUpLoad(List<ApplyCarBean> carList) {
        isLoading = false;
        swipeRefreshLayout.setRefreshing(false);
        if(carList==null||carList.size()==0){
            adapter.setHasMoreData(false);
        }else{
            data.addAll(carList);
        }
        adapter.notifyDataSetChanged();
        adapter.notifyItemRemoved(adapter.getItemCount());
    }

}
